package com.weitu.contractor.controller;

import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.contractor.entity.ContractorInfo;
import com.weitu.contractor.service.ContractorInfoService;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * @author LiMeng 2017/4/5
 */
@Controller
@RequestMapping(value = "contractor/contractorInfo")
public class ContractorInfoController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "contractor/contractorInfo";
    private final ContractorInfoService contractorInfoService;
    @Autowired
    public ContractorInfoController(ContractorInfoService contractorInfoService) {
        this.contractorInfoService = contractorInfoService;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return MODULE_BATH_PATH + "/list";
    }

    @RequestMapping(value = "/simpleList", method = RequestMethod.GET)
    public String simpleList() {
        return MODULE_BATH_PATH + "/simpleList";
    }

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object listAjax(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);//添加查询条件

        int count = contractorInfoService.queryCount(paramMap);//根据条件查询数量

        DataGridPagination dataGridPagination = DataGridPagination.getInstance(request, count);//获取分页对象
        PageView pageView = dataGridPagination.getPageView();//生成pageView分页对象
        //未传sort参数是采用添加时间排序
        if (paramMap.get("sort") == null) {
            pageView.setSortColumn("addTime");
        }
        paramMap.put("pageView", pageView);//加入分页参数对象

        List<ContractorInfo> contractorInfoList = contractorInfoService.queryList(paramMap);//根据参数查询list

        return dataGridPagination.flushDataGridData(contractorInfoList);
    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public String add() {
        return MODULE_BATH_PATH + "/add";
    }

    @SystemLog(module = "承包商信息", methods = "添加承包商")
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public AjaxResult addAjax(ContractorInfo contractorInfo) throws Exception {
        if (contractorInfoService.checkIfExist(contractorInfo.getName(), contractorInfo.getCode())) {
            return fail("名称和代码不能重复！");
        }
        String id = IDGenerator.getId();
        contractorInfo.setId(id);

        putAddInfo(contractorInfo);
        putUpdateInfo(contractorInfo);
        if (contractorInfoService.insertIfNotExist(contractorInfo)) {
            return success("承包商信息保存成功！", contractorInfo);
        } else {
            return fail("承包商信息保存失败！");
        }
    }

    @RequestMapping(value = "edit", method = RequestMethod.GET)
    public String editAjax(Model model, String id) throws Exception {
        model.addAttribute("contractor", contractorInfoService.selectById(id));
        return MODULE_BATH_PATH + "/edit";
    }

    @SystemLog(module = "承包商信息", methods = "编辑承包商")
    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public AjaxResult editAjax(ContractorInfo contractorInfo) throws Exception {
        putUpdateInfo(contractorInfo);
        if (contractorInfoService.updateByIdSelective(contractorInfo)) {
            return success("承包商信息更新成功！");
        } else {
            return fail("承包商信息更新失败！");
        }
    }

    @SystemLog(module = "承包商信息", methods = "删除承包商")
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public AjaxResult deleteAjax(String id) throws Exception {
        if (contractorInfoService.deleteById(id) == 1) {
            return success("删除承包商信息成功！");
        } else {
            return fail("删除承包商信息失败！");
        }
    }

    /**
     * 添加添加时间、人、部门信息参数
     *
     * @param contractorInfo 实体
     */
    private void putAddInfo(ContractorInfo contractorInfo) {
        String nowTime = DateTimeUtil.formatterDateTime();
        contractorInfo.setAddTime(nowTime);
        contractorInfo.setAddUserId(getSessionUser().getId());
        contractorInfo.setAddDeptId(getSessionUser().getDepartment().getId());
    }

    /**
     * 添加编辑时间、人、部门信息参数
     *
     * @param contractorInfo 实体
     */
    private void putUpdateInfo(ContractorInfo contractorInfo) {
        String nowTime = DateTimeUtil.formatterDateTime();
        contractorInfo.setUpdateTime(nowTime);
        contractorInfo.setUpdateUserId(getSessionUser().getId());
        contractorInfo.setUpdateDeptId(getSessionUser().getDepartment().getId());
    }
}
